# 1、导包
import time
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.support.select import Select

# 2、实例化浏览器对象
driver_path = "C:/Users/Lenovo/AppData/Local/Programs/Python/Python310/geckodriver.exe"
service = Service(executable_path=driver_path)
driver = webdriver.Firefox(service=service)

# TODO 下拉框
# 31、 需求 - 传统方式
'''
driver.get("file:///E:/project/learn-python/html%E5%9F%BA%E7%A1%80/3-%E8%A1%A8%E5%8D%95%E6%8F%90%E4%BA%A4.html")
# ① 选择 "广州"
driver.find_element(By.CSS_SELECTOR, '[value="gz"]').click()

# ② 暂停2秒，选择 "上海"
time.sleep(2)
driver.find_element(By.CSS_SELECTOR, '[value="sh"]').click()

# ③ 暂停2秒，选择 "北京"
time.sleep(2)
driver.find_element(By.CSS_SELECTOR, '[value="bj"]').click()
'''
# 32、 需求 - select类方法
'''
select = Select(driver.find_element(By.ID, "addr"))             # 实例化下拉框选择对象

# ① 根据 索引 定位
select.select_by_index(2)
time.sleep(2)

# ② 根据 value值 定位
select.select_by_value("sh")
time.sleep(2)

# ③ 根据 显示文本 定位
select.select_by_visible_text("深圳")
time.sleep(2)
'''
# TODO 弹出框
# 3、 需求 - 系统弹窗
'''
driver.get("file:///E:/project/learn-python/html%E5%9F%BA%E7%A1%80/3-%E8%A1%A8%E5%8D%95%E6%8F%90%E4%BA%A4.html")
# ① 点击 alert 按钮
# driver.find_element(By.CSS_SELECTOR, '[value="alert"]').click()     # 警告框
# driver.find_element(By.ID, 'conf').click()                          # 确认框
driver.find_element(By.CSS_SELECTOR, '[value="prompt"]').click()    # 提示框

# ② 关闭警告框（只要是系统弹窗，处理方法均为以下abc步骤）
# a.切换到弹窗
alert = driver.switch_to.alert
# b.获取弹窗信息（可选）：获取弹窗信息必须在处理弹窗操作之前
print("弹窗信息是：", alert.text)
time.sleep(2)
# c.去除弹窗（同意/移除）
# alert.accept()   # 同意
alert.dismiss()  # 移除
time.sleep(2)

# ③ 输入用户名：admin
time.sleep(2)
driver.find_element(By.ID, "user").send_keys("admin")
'''
# TODO 滚动条
driver.get("file:///E:/project/learn-python/html%E5%9F%BA%E7%A1%80/%E6%BB%9A%E5%8A%A8%E6%9D%A1.html")
# 3、需求：打开页面，暂停2秒后，滚动条拉到最底层
time.sleep(2)
js_down = "window.scrollTo(0, 2000)"            # 向下
driver.execute_script(js_down)

time.sleep(2)
js_up = "window.scrollTo(0, 0)"                 # 向上（反向，只需要坐标归零）
driver.execute_script(js_up)